/********* ImageEditor.m Cordova Plugin Implementation *******/

#import <Cordova/CDV.h>

@interface ImageEditor : CDVPlugin {
    // Member variables go here.
    CDVInvokedUrlCommand* mCommand;
}
- (void)imageEditor:(CDVInvokedUrlCommand*)command;
@end

@implementation ImageEditor

- (void)imageEditor:(CDVInvokedUrlCommand*)command
{
    CDVPluginResult* pluginResult = nil;
    NSString* echo = [command.arguments objectAtIndex:0];
    
    if (echo != nil && [echo length] > 0) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:echo];
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
    }
    mCommand = command;
    //添加字典，将ImagePath的值通过key值设置传递给主视图控制器
    NSDictionary *dict =[[NSDictionary alloc]initWithObjectsAndKeys:echo,@"ImagePath",nil];
    //创建通知
    NSNotification *notification =[NSNotification notificationWithName:@"ImagePath" object:nil userInfo:dict];
    //通过通知中心发送通知让主页面处理
    [[NSNotificationCenter defaultCenter] postNotification:notification];
    //添加监听 收取编辑完成的ImagePath
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editFinishImagePath:)name:@"editFinishImagePath" object:nil];
}

//接收编辑完成图片的地址
- (void)editFinishImagePath:(NSNotification *)imagePathNot
{
    CDVPluginResult* pluginResult = nil;
    pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:imagePathNot.userInfo[@"ImagePath"]];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:mCommand.callbackId];
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

@end
